<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        img {
            vertical-align: top;
        }

        a {
            text-decoration: none;
            color: inherit;
        }

        li {
            list-style: none;
        }

        .wrap {
            width: 1226px;
            margin: 0 auto;
            margin-top: 50px;
            position: relative;
        }

        .nav {
            text-align: center;
            line-height: 40px;
            display: flex;
            position: absolute;
            right: 20px;
            bottom: 10px;
            z-index: 99;
        }

        .nav li {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            border: 1px solid #333;
            margin: 5px;
        }


        .nav .active {
            background-color: pink;
            color: #fff;
        }

        .tabCon {
            height: 460px;
            position: relative;
        }

        .tabCon .part {
            box-sizing: border-box;
            text-align: center;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: all 0.8s;
        }

        .tabCon .part img {
            width: 100%;
        }

        .tabCon .show {
            opacity: 1;
            z-index: 1;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="nav">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="tabCon">
            <div class="part show"><a href=""><img src="../images/1.webp" alt=""></a></div>
            <div class="part"><a href=""><img src="../images/2.jpg" alt=""></a></div>
            <div class="part"><a href=""><img src="../images/3.webp" alt=""></a></div>
            <div class="part"><a href=""><img src="../images/4.webp" alt=""></a></div>
            <div class="part"><a href=""><img src="../images/5.webp" alt=""></a></div>
        </div>
    </div>
</body>
<script>
    // 点击导航 => 被点击的导航变为活跃状态,其他导航回归默认样式
    // (1) 每次点击 先清除上一个被点击的导航的样式  再把当前的导航变为活跃状态
    // (2) 不管之前哪一个被点击 => 清除所有导航的活跃状态的样式 => 再把自己变为活跃状态

    var wrap = document.getElementsByClassName("wrap")[0];
    var liList = document.querySelectorAll(".nav li");
    var partList = document.querySelectorAll(".tabCon .part");

    var index = 0; //全局变量 记录轮播的下标
    var timer = null;

    autoPlay(); //页面加载时启动轮播

    for (let i = 0; i < liList.length; i++) {
        let li = liList[i];
        li.onclick = function () {
            // debugger; // 点击时触发

            index = i; //点击时 index记录下标 => 下次从当前位置滚动

            // 清除所有导航的活跃状态的样式  tab栏全部隐藏
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
                partList[j].classList.remove("show");
            }
            // this.className = "active";  // 被点击的导航变为活跃状态
            liList[i].className = "active";  // 被点击的导航变为活跃状态
            partList[i].classList.add("show"); // 对应下标的tab栏显示
        }
    }

    wrap.onmouseover = function () {
        clearInterval(timer);
    }

    wrap.onmouseout = function () {
        autoPlay();
    }

    function autoPlay() {
        clearInterval(timer);
        timer = setInterval(function () {
            index++;
            if (index >= liList.length) {
                index = 0;
            }

            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
                partList[j].classList.remove("show");
            }
            // this.className = "active";  //对应下标导航变为活跃状态
            liList[index].className = "active";  //对应下标导航变为活跃状态
            partList[index].classList.add("show"); // 对应下标的tab栏显示
        }, 3000);
    }


</script>

</html>